const Controller = require('egg').Controller;


class dashboard extends Controller {
    /**
     * 菜单跳转
     * @returns {Promise<void>}
     */
    async to(){
        const {app,ctx,service} = this;
        await ctx.render('/admin/dashboard.ejs',{});
    }

    async allAchievments(){
        const {app,ctx,service} = this;
        const {date} = ctx.request.query;
        const {start,end } = await this.dateUtils(date);
        const Op = app.Sequelize.Op;

        let date_sql = ` and DATE_FORMAT( updated_at, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) `;
        if(!app.lodash.isEmpty(start) && !app.lodash.isEmpty(end)){
            date_sql = ` and updated_at between '${start}' and '${end}' `
        }
        let sql = `
        SELECT count(*) as total FROM fd_order WHERE deleted = '0' and status = '12'
        ${date_sql}        
        `;

        const sequelize_query_param = {raw:true,type:app.Sequelize.QueryTypes.SELECT};
        let allTotal = await ctx.model.query(sql,sequelize_query_param);

        ctx.body = {
            code : 200,
            data : {
                total:allTotal[0].total
            },
            msg : ''
        }
    }

    async allAchievmentsLine(){
        const {app,ctx,service} = this;
        const {type,year,month} = ctx.request.query;

        let data;
        const sequelize_query_param = {raw:true,type:app.Sequelize.QueryTypes.SELECT};
        if(type == 0){
            if(app.lodash.isEmpty(year)){
                ctx.body = {
                    code : 401,
                    data : {
                        xAxis:[],
                        yAxis:[]
                    },
                    msg : ''
                }
                return;
            }
            let sql = `
            select count(*) as total,DATE_FORMAT( updated_at, '%m' ) as month from fd_order 
            where deleted = '0' and status = '12'
            and DATE_FORMAT( updated_at, '%Y' ) = '${year}'
            `;
            data = await ctx.model.query(sql,sequelize_query_param);
            let mounts = ['01','02','03','04','05','06','07','08','09','10','11','12'];
            let datas = ['0','0','0','0','0','0','0','0','0','0','0','0'];
            data.forEach((item,index)=>{
                let ind = mounts.indexOf(item.month);
                if(ind != -1){
                    datas[ind] = item.total;
                }
            })
            ctx.body = {
                code : 200,
                data : {
                    xAxis:mounts,
                    yAxis:datas
                },
                msg : ''
            }
        }else if(type == 1){
            if(app.lodash.isEmpty(month)){
                ctx.body = {
                    code : 401,
                    data : {
                        xAxis:[],
                        yAxis:[]
                    },
                    msg : ''
                }
                return;
            }
            let sql = `
            select count(*) as total,DATE_FORMAT( updated_at, '%d' ) as day from fd_order 
            where deleted = '0' and status = '12'
            and DATE_FORMAT( updated_at, '%Y-%m' ) = '${month}'
            `;
            data = await ctx.model.query(sql,sequelize_query_param);
            let xAxis = await ctx.helper.getDateXAxis(month);
            let yAxis = new Array(xAxis.length).fill(0);
            data.forEach((item,index)=>{
                let ind = xAxis.indexOf(item.day);
                if(ind != -1){
                    yAxis[ind] = item.total;
                }
            })
            ctx.body = {
                code : 200,
                data : {
                    xAxis:xAxis,
                    yAxis:yAxis
                },
                msg : ''
            }
        }
    }

    async oneAchievments(){
        const {app,ctx,service} = this;
        const {date,center,store} = ctx.request.query;
        const {start,end } = await this.dateUtils(date);

        let date_sql = ` and DATE_FORMAT( a.updated_at, '%Y%m' ) = DATE_FORMAT( CURDATE() , '%Y%m' ) `;
        if(!app.lodash.isEmpty(start) && !app.lodash.isEmpty(end)){
            date_sql = ` and a.updated_at between '${start}' and '${end}' `
        }
        let center_sql = '';
        if(!app.lodash.isEmpty(center)){
            center_sql = ` and b.center_id = '${center}' `;
        }
        let store_sql = '';
        if(!app.lodash.isEmpty(store)){
            store_sql = ` and b.id = '${store}' `;
        }
        let sql = `
        SELECT count(a.id) as total FROM fd_order a 
         left join fd_store b on b.id = a.store_id 
         WHERE a.deleted = '0' and a.status = '12'
        ${date_sql}  ${center_sql} ${store_sql}      
        `;

        const sequelize_query_param = {raw:true,type:app.Sequelize.QueryTypes.SELECT};
        let allTotal = await ctx.model.query(sql,sequelize_query_param);

        ctx.body = {
            code : 200,
            data : {
                total:allTotal[0].total
            },
            msg : ''
        }
    }

    async oneAchievmentsLine(){
        const {app,ctx,service} = this;
        const {type,year,month,center,store} = ctx.request.query;

        let center_sql = '';
        if(!app.lodash.isEmpty(center)){
            center_sql = ` and b.center_id = '${center}' `;
        }
        let store_sql = '';
        if(!app.lodash.isEmpty(store)){
            store_sql = ` and b.id = '${store}' `;
        }

        let data;
        const sequelize_query_param = {raw:true,type:app.Sequelize.QueryTypes.SELECT};
        if(type == 0){
            if(app.lodash.isEmpty(year)){
                ctx.body = {
                    code : 401,
                    data : {
                        xAxis:[],
                        yAxis:[]
                    },
                    msg : ''
                }
                return;
            }
            let sql = `
            select count(*) as total,DATE_FORMAT( a.updated_at, '%m' ) as month from fd_order a
            left join fd_store b on b.id = a.store_id
            where a.deleted = '0' and a.status = '12'
            and DATE_FORMAT( a.updated_at, '%Y' ) = '${year}' ${center_sql} ${store_sql}
            `;
            data = await ctx.model.query(sql,sequelize_query_param);
            let mounts = ['01','02','03','04','05','06','07','08','09','10','11','12'];
            let datas = ['0','0','0','0','0','0','0','0','0','0','0','0'];
            data.forEach((item,index)=>{
                let ind = mounts.indexOf(item.month);
                if(ind != -1){
                    datas[ind] = item.total;
                }
            })
            ctx.body = {
                code : 200,
                data : {
                    xAxis:mounts,
                    yAxis:datas
                },
                msg : ''
            }
        }else if(type == 1){
            if(app.lodash.isEmpty(month)){
                ctx.body = {
                    code : 401,
                    data : {
                        xAxis:[],
                        yAxis:[]
                    },
                    msg : ''
                }
                return;
            }
            let sql = `
            select count(*) as total,DATE_FORMAT( a.updated_at, '%d' ) as day from fd_order a
            left join fd_store b on b.id = a.store_id
            where a.deleted = '0' and a.status = '12'
            and DATE_FORMAT( a.updated_at, '%Y-%m' ) = '${month}' ${center_sql} ${store_sql}
            `;
            data = await ctx.model.query(sql,sequelize_query_param);
            let xAxis = await ctx.helper.getDateXAxis(month);
            let yAxis = new Array(xAxis.length).fill(0);
            data.forEach((item,index)=>{
                let ind = xAxis.indexOf(item.day);
                if(ind != -1){
                    yAxis[ind] = item.total;
                }
            })
            ctx.body = {
                code : 200,
                data : {
                    xAxis:xAxis,
                    yAxis:yAxis
                },
                msg : ''
            }
        }
    }


    async dateUtils(date){
        const {app,ctx,service} = this;
        let start = '';
        let end = '';
        if(!app.lodash.isEmpty(date)){
            let index = date.indexOf('~');
            if(index == -1){
                return {start,end};
            }
            start = date.substring(0,index);
            end = date.substring(index+1,date.length);
        }
        return {start,end};
    }
}

module.exports = dashboard;
